#!/bin/sh
# preinst script for Wazuh

set -e

# configuration variables
DIR="/var/ossec"
WAZUH_TMP_DIR="${DIR}/packages_files/manager_config_files"
VERSION="$2"
MAJOR=$(echo "$VERSION" | cut -dv -f2 | cut -d. -f1)

# environment configuration
if [ ! -d ${WAZUH_TMP_DIR} ]; then
    mkdir -p ${WAZUH_TMP_DIR}
else
    rm -rf ${WAZUH_TMP_DIR}
    mkdir -p ${WAZUH_TMP_DIR}
fi

case "$1" in
    install|upgrade)

        if [ "$1" = "upgrade" ]; then
            if command -v systemctl > /dev/null 2>&1 && systemctl > /dev/null 2>&1 && systemctl is-active --quiet wazuh-manager > /dev/null 2>&1; then
                systemctl stop wazuh-manager.service > /dev/null 2>&1
                touch ${WAZUH_TMP_DIR}/wazuh.restart
            elif command -v service > /dev/null 2>&1 && service wazuh-manager status 2>/dev/null | grep "is running" > /dev/null 2>&1; then
                service wazuh-manager stop > /dev/null 2>&1
                touch ${WAZUH_TMP_DIR}/wazuh.restart
            fi

            if [ -d ${DIR}/logs/ossec ]; then
                mv ${DIR}/logs/ossec ${DIR}/logs/wazuh
            fi

            if [ -d ${DIR}/queue/ossec ]; then
                mv ${DIR}/queue/ossec ${DIR}/queue/sockets
            fi

            # Delete old API backups
            if [ -d ${DIR}/~api ]; then
                rm -rf ${DIR}/~api
            fi

            # Get old package version
            if [ -f /etc/ossec-init.conf ]; then
                . /etc/ossec-init.conf
            else
                VERSION=$(${DIR}/bin/wazuh-control info -v)
            fi

            # Get the major and minor version
            MAJOR=$(echo $VERSION | cut -dv -f2 | cut -d. -f1)
            MINOR=$(echo $VERSION | cut -d. -f2)

            # Delete 3.X Wazuh API service
            if [ "$MAJOR" = "3" ] && [ -d ${DIR}/api ]; then
                if command -v systemctl > /dev/null 2>&1 && systemctl > /dev/null 2>&1 && \
                  systemctl list-unit-files --type service | grep wazuh-api; then
                    systemctl stop wazuh-api.service > /dev/null 2>&1
                    systemctl disable wazuh-api.service > /dev/null 2>&1
                    rm -f /etc/systemd/system/wazuh-api.service || true
                fi

                if command -v service > /dev/null 2>&1 && service status wazuh-api > /dev/null 2>&1 ; then
                    service wazuh-api stop > /dev/null 2>&1
                    update-rc.d wazuh-api remove > /dev/null 2>&1
                    rm -f /etc/rc.d/init.d/wazuh-api || true
                fi
            fi
        fi

        if [ ! -z "$2" ] && [ ! -f ${DIR}/etc/ossec.conf ] ; then
            touch ${WAZUH_TMP_DIR}/create_conf
        fi

        if [ "$1" = "upgrade" ]; then
            # RBAC database
            if [ -f ${DIR}/api/configuration/security/rbac.db ]; then
                cp -fp ${DIR}/api/configuration/security/rbac.db ${WAZUH_TMP_DIR}/rbac.db
            fi

            # API configuration file
            if [ -f ${DIR}/api/configuration/api.yaml ]; then
                cp -fp ${DIR}/api/configuration/api.yaml ${WAZUH_TMP_DIR}/api.yaml
            fi

            # Agent-groups files
            if [ -d ${DIR}/queue/agent-groups ]; then
                mv -f ${DIR}/queue/agent-groups ${WAZUH_TMP_DIR}/
            fi
        fi

        # Delete old service
        if [ -f /etc/init.d/ossec ]; then
          rm /etc/init.d/ossec
        fi

        if [ -d ${DIR}/etc/lists ]; then
          cp -rp ${DIR}/etc/lists ${WAZUH_TMP_DIR}/lists
        fi

        if [ -f ${DIR}/etc/client.keys ]; then
            cp -p ${DIR}/etc/client.keys ${WAZUH_TMP_DIR}/client.keys
        fi

        if [ -f ${DIR}/etc/local_internal_options.conf ]; then
            cp -p ${DIR}/etc/local_internal_options.conf ${WAZUH_TMP_DIR}/local_internal_options.conf
        fi

        if [ -f ${DIR}/etc/rules/local_rules.xml ]; then
            cp -p ${DIR}/etc/rules/local_rules.xml ${WAZUH_TMP_DIR}/local_rules.xml
        fi

        if [ -f ${DIR}/etc/decoders/local_decoder.xml ]; then
            cp -p ${DIR}/etc/decoders/local_decoder.xml ${WAZUH_TMP_DIR}/local_decoder.xml
        fi

        if [ -f ${DIR}/etc/ossec.conf ]; then
            cp -p ${DIR}/etc/ossec.conf ${WAZUH_TMP_DIR}/ossec.conf
        fi

        if [ -d ${DIR}/etc/shared ]; then
            cp -rp ${DIR}/etc/shared ${WAZUH_TMP_DIR}/group
        fi

        if [ -d ${DIR}/var/db/agents ]; then
            rm -rf ${DIR}/var/db/agents
        fi

        # Remove plain-text agent information if exists
        if [ -d ${DIR}/queue/agent-info ]; then
            rm -rf ${DIR}/queue/agent-info/* > /dev/null 2>&1
        fi

        if [ -d ${DIR}/queue/rootcheck ]; then
            rm -rf ${DIR}/queue/rootcheck/* > /dev/null 2>&1
        fi

        # Remove groups backup content if exists
        if [ -d ${DIR}/backup/groups ]; then
            rm -rf ${DIR}/backup/groups/* > /dev/null 2>&1
        fi
    ;;

    abort-upgrade)

    ;;

    *)
        echo "preinst called with unknown argument \`$1'" >&2
        exit 0

    ;;

esac

exit 0
